<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

	public function Home()
	{
		parent::__construct();
		$this->load->library('ion_auth');
		$this->lang->load('awb');
		$this->config->load('awb');
	}
	public function draw()
	{ 
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login', 'refresh');
		}
		$this->load->model('file_model');
		$file_id = $this->input->get("f_id");
		$file_data =$this->file_model->get_file_by_file_id($file_id);
		$group_data =$this->file_model->get_group_by_file_id($file_id);
		// $this->load->view("db_header_view");
		$data = array();
		$data['toolbox_dialog'] = $this->load->view("util/creation-dialog-shape",$data,true);	
		// $this->config->load('awb');
		$this->load->model('stamp_model');		
		$data_stamp['stamp'] = $this->stamp_model->get_all_stamp();
		$data['recover_dialog'] = $this->load->view("util/creation-dialog-recover",$data,true);
		$data['toolboxStamp_dialog'] = $this->load->view("util/creation-dialog-stamp",$data_stamp,true);
		//ulum
		$data['toolboxBubble_dialog'] = $this->load->view("util/creation-dialog-bubble",array(),true);
		$data['toolboxSticky_dialog'] = $this->load->view("util/creation-dialog-sticky",array(),true);
		//ulum
		$data['upload_dialog'] = $this->load->view("util/creation-dialog-popup-image",$data,true);
		$data['create_page_dialog'] = $this->load->view("util/creation-dialog-page",$data,true);
		$data['uploadFile_dialog'] = $this->load->view("util/creation-dialog-file",$data,true);
		$data['chart_dialog'] = $this->load->view("util/creation-dialog-chart",$data,true);
		$data['initial_table_dialog'] = $this->load->view("util/creation-dialog-initial-table",$data,true);
		$data['pick_color_dialog'] = $this->load->view("util/creation-dialog-pick-color",$data,true);
		$data['text_logo_dialog'] = $this->load->view("util/creation-dialog-text-logo",$data,true);
		$data['page_file_dialog'] = $this->load->view("util/creation-dialog-page-file",$data,true);
		$data['save_as_dialog'] = $this->load->view("util/creation-dialog-save-as",$data,true);
		$data['property_dialog'] = $this->load->view("util/creation-dialog-property",$data,true);
		$data['open_file_dialog'] = $this->load->view("util/creation-dialog-open-file",$data,true);
		$data['open_file_template_dialog'] = $this->load->view("util/creation-dialog-open-file-template",$data,true);
		$data['rotation_dialog'] = $this->load->view("util/creation-dialog-rotation",$data,true);
		$data['lock_object_dialog'] = $this->load->view("util/creation-dialog-lock-object",$data,true);
		$data['unlock_object_dialog'] = $this->load->view("util/creation-dialog-unlock-object",$data,true);
		$data['dialog_list_user'] = $this->load->view("util/dialog-list-user",$data,true);
		$data['file_id'] = $file_id;
		$data['file_name'] = $file_data->file_name;
		$data['group_name'] = $group_data['AWBGROUP_NAME']." - ".$group_data['WS_NAME'];
		$data['is_template'] = $file_data->is_template;
		$data['page_id'] = 1;
		$data['user'] = $this->ion_auth->user()->row();
		$data['last_time'] = time();
		$this->load->view("draw",$data);
	}
	public function player()
	{
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login', 'refresh');
		}
		$this->load->model('file_model');
		$this->load->model('user_model');
		$file_id = $this->input->get("f_id");
		$page_id = $this->input->get("p_id");
		$file_data =$this->file_model->get_file_by_file_id($file_id);
		$file_history =$this->file_model->get_file_history_since(0,$file_id,$page_id);
		foreach($file_history as $single_history)
		{
			if($single_history->operation=="ADD")
				$single_history->previous_content  = json_decode($single_history->changed_content );
			else
				$single_history->previous_content = json_decode($single_history->previous_content);
			
			if($single_history->operation!="DEL")
			{
				$single_history->changed_content  = json_decode($single_history->changed_content );
				$single_history->previous_content  = $single_history->changed_content ;
			}
			//Nanda - Notes: Change attribute type to attribute object_type
			// $single_history->type = $single_history->previous_content->type;
			$single_history->object_type = $single_history->previous_content->object_type;
			$single_history->user = $this->user_model->get_user_by_id($single_history->user_id);
			
			//Nanda - Notes: Change attribute type to attribute object_type
			// if($single_history->type == "text" )
			if($single_history->object_type == "text" )
			{
				$single_history->previous_content->content = htmlentities($single_history->previous_content->content);
				if(!empty($single_history->changed_content))
					$single_history->changed_content->content = htmlentities($single_history->changed_content->content);
			}
		}
		
		// $this->load->view("db_header_view");
		$data = array();
		$this->config->load('awb');
		
		$data['file_id'] = $file_id;
		$data['file_name'] = $file_data->file_name;
		$data['file_history'] = $file_history;
		$data['page_id'] =$page_id;
		$this->load->view("draw_player",$data);
	}
	
	public function draw_preview()
	{
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login', 'refresh');
		}
		$this->load->model('file_model');
		$file_id = $this->input->get("f_id");
		$file_data =$this->file_model->get_file_by_file_id($file_id);
		// $this->load->view("db_header_view");
		$data = array(); 	
		$this->config->load('awb');
		$this->load->model('stamp_model');		
		$data_stamp['stamp'] = $this->stamp_model->get_all_stamp(); 
		$data['file_id'] = $file_id;
		$data['file_name'] = $file_data->file_name;
		$data['is_template'] = $file_data->is_template;
		$data['page_id'] = 1;
		$data['user'] = $this->ion_auth->user()->row();
		$data['last_time'] = time();
		$this->load->view("draw_preview",$data);
	}
	
	public function print_page()
	{
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login', 'refresh');
		}
		$this->load->model('file_model');
		$file_id = $this->input->get("f_id"); 
		$file_data =$this->file_model->get_file_by_file_id($file_id);
		// print_r($file_data);
		$data['file_id'] = $file_id;
		$data['page_id'] = 1;
		$data['file_name'] = $file_data->file_name;
		$data['user'] = $this->ion_auth->user()->row();
		$data['last_time'] = time();
		$data['file_data'] = $file_data->is_template;
		
		$this->load->view("print",$data);
	}
	
	
	public function index()
	{	
		if (!$this->ion_auth->logged_in())
		{
			$id = $this->input->get('ID');
			$ped = $this->input->get('PED');
			if(!empty($id) && !empty($ped))
			{
				redirect('auth/login?ID='.$id.'&PED='.$ped, 'refresh');
			}
			else
				redirect('auth/login', 'refresh');
		}
		$this->load->model('group_model');
		$this->load->model('file_model');
		// echo "<pre>";
		$user = $this->ion_auth->user()->row();
		// print_r($user);
		$group = $this->group_model->get_group_by_user_id($user->pk_id);
		// print_r($group);
		// echo "</pre>";
		$files = array();
		foreach($group as $single_group)
		{
			$group_file = $this->group_model->get_file_by_group_id($single_group['AWBGROUP_ID'],true);
			if(empty($group_file))
			{
				//no file available,make a new one
				$new_data = $this->file_model->make_blank_file($single_group['AWBGROUP_ID'],"Default File");
				
				$new_file_class = new stdClass();
				$new_file_class->file_id = $new_data['file_id'];
				$new_file_class->file_name = $new_data['file_name'];
				$new_file_class->group_id = $new_data['group_id'];
				$new_file_class->created_on = $new_data['created_on'];
				$new_file_class->content = $new_data['content'];
				$new_file_class->last_update = $new_data['last_update'];
				array_push($files,$new_file_class);
			}
			else
			{
				foreach($group_file as $single_group_file)
				{
					array_push($files,$single_group_file);
				}
			}
		}
		
		$group_content="";
		$group_array = array();
		foreach($group as $value){ 
				$group_array[$value['AWBGROUP_ID']] = $value['AWBGROUP_NAME']."(".$value['WS_NAME'].")";
		} 
		natcasesort($group_array);		
		
		$group_content .= '<option selected="selected" value=0>'.$this->lang->line('caption_all').'</option>';
		foreach($group_array as $key=> $value){ 
			$group_content .= "<option value=".$key.">".$value."</option>";
		}
		$file_content="";
		$file_array = array();
		foreach($files as $value){ 
				$value->content= json_decode($value->content);
				$value->page_count = sizeof($value->content->pages);
				$value->content="";
				$value->group=$group_array[$value->group_id];
				$value->created_on=date('d/m/Y H:m', $value->created_on);
				$value->last_update=date('d/m/Y H:m', $value->last_update);
				$file_array[$value->file_id] = $value->file_name;
				
		} 
		natcasesort($file_array);		
		foreach($file_array as $key=> $value){ 
		 $file_content .= "<option value=".$key.">".$value."</option>";
		}
		
		$data["json"]=json_encode($files);
		$data["files_object"]=$files;
		$data["files"]=$file_content;
		$data["groups"]=$group_content;
		$data["u_id"] = $user->id;
		$data["username"] = $user->username;
		$this->load->view('workspace_view',$data);
	}
	
	public function test(){
		$this->load->view('test');
	}
	
}
